//
//  ChatKeyboardView.m
//  WChat
//
//  Created by lee on 2018/8/7.
//  Copyright © 2018年 mjsfax. All rights reserved.
//

#import "ChatKeyboardView.h"
@interface ChatKeyboardView()
@property (strong, nonatomic) NSArray *titles;
@property (strong, nonatomic) NSMutableArray *buttons;
@end

@implementation ChatKeyboardView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        for (NSString *title in self.titles) {
            [self addButton:title];
        }
    }
    return self;
}

- (void)addButton:(NSString *)title {
    UIButton *button = [[UIButton alloc] init];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor grayColor]];
    [button.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    [self.buttons addObject:button];
}

- (void)buttonClick:(UIButton *)sender {
    NSLog(@"click%d=====%d",[self.buttons indexOfObject:sender],sender.tag);
    if (self.actionBlock) {
        self.actionBlock(sender.tag);
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat side = 80;
    CGFloat orx = 10;
    CGFloat ory = 10;
    
    NSInteger rowCount = 2;
    NSInteger colCount = 4;
    
    CGFloat margin = (SCREENWIDTH-2*orx-4*side) / 3;
    for (int i=0; i<self.buttons.count; i++) {
        CGFloat x = orx + (margin + side) * (i % colCount);
        CGFloat y = ory + i / colCount * (side+10);
        UIButton *btn = self.buttons[i];
        btn.tag = i;
        btn.frame = CGRectMake(x, y, side, side);
    }
}

- (NSArray *)titles {
    if (!_titles) {
        _titles = @[@"照片",@"视频通话",@"拍摄",@"位置",@"红包",@"转账",@"输入语音"];
    }
    return _titles;
}

- (NSMutableArray *)buttons {
    if (!_buttons) {
        _buttons = [NSMutableArray arrayWithCapacity:self.titles.count];
    }
    return _buttons;
}

@end
